﻿
using Management.Systeam.Domaina;
using Management.Systeam.Infrastructure.Interface;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.Logging;
using NPOI.SS.Formula.Functions;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.Collections.Generic;
using System.Transactions;
using static NPOI.HSSF.Util.HSSFColor;

namespace Management.Systeam.Read.Apis.Controllers.ExIM
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ExportAllController : ControllerBase
    {

        IBaseRepository<Users> Users;
        IBaseRepository<Role> Role;
        IBaseRepository<Resources> Resources;
        IBaseRepository<DepartMents> DepartMents;
        IBaseRepository<ProductOutAllow> ProductOutAllow;
        IBaseRepository<InventoryAudit> InventoryAudit;

        public ExportAllController(IBaseRepository<Users> Users, IBaseRepository<Role> Role, IBaseRepository<Resources> Resources, IBaseRepository<DepartMents> DepartMents, IBaseRepository<ProductOutAllow> ProductOutAllow, IBaseRepository<InventoryAudit> InventoryAudit)
        {
            this.Users = Users;
            this.Role = Role;
            this.Resources = Resources;
            this.DepartMents = DepartMents;
            this.ProductOutAllow = ProductOutAllow;
            this.InventoryAudit = InventoryAudit;
        }



        /// <summary>
        /// 用户批量导出
        /// </summary>
        /// <param name="Index"></param>
        /// <param name="Size"></param>
        /// <param name="Name"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> ExportAll(string? Ids)
        {
            IWorkbook workbook = new XSSFWorkbook();
            ISheet sheet = workbook.CreateSheet("工作簿");
            using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                try
                {
                    IRow col = sheet.CreateRow(0);
                    col.CreateCell(0).SetCellValue("用户名");
                    col.CreateCell(1).SetCellValue("工号");
                    col.CreateCell(2).SetCellValue("真名");
                    col.CreateCell(3).SetCellValue("邮箱");
                    col.CreateCell(4).SetCellValue("手机");
                    col.CreateCell(5).SetCellValue("固定电话");
                    col.CreateCell(6).SetCellValue("创建时间");
                    col.CreateCell(7).SetCellValue("登录次数");
                    col.CreateCell(8).SetCellValue("部门");
                    col.CreateCell(9).SetCellValue("角色");
                    List<Users> uselist = new List<Users>();
                    var data = Ids.Split(",").Select(x => int.Parse(x));
                    foreach (var id in data)
                    {
                        var slist = Users.GetQuery().Where(x => x.UId == id && !x.IsDel).ToList();
                        foreach (var item in slist)
                        {
                            if (item != null)
                                uselist.Add(item);
                        }
                        for (int i = 0; i < uselist.Count(); i++)
                        {
                            IRow scol = sheet.CreateRow(i + 1);
                            scol.CreateCell(0).SetCellValue(uselist[i].User_Name);
                            scol.CreateCell(1).SetCellValue(uselist[i].User_WorkCode);
                            scol.CreateCell(2).SetCellValue(Convert.ToDouble(uselist[i].Login_Number));
                            scol.CreateCell(3).SetCellValue(uselist[i].User_Email);
                            scol.CreateCell(4).SetCellValue(Convert.ToDouble(uselist[i].User_Phone));
                            scol.CreateCell(5).SetCellValue(Convert.ToDouble(uselist[i].User_FixedPhone));
                            scol.CreateCell(6).SetCellValue(uselist[i].CreateTime.ToString().Substring(0, 10));
                            scol.CreateCell(7).SetCellValue(Convert.ToDouble(uselist[i].Login_Number));
                            scol.CreateCell(8).SetCellValue(Convert.ToDouble(uselist[i].Department_Id));
                            scol.CreateCell(9).SetCellValue(Convert.ToDouble(uselist[i].Role_Id));
                        }
                    }

                }
                catch (Exception)
                {
                    throw;
                }
                byte[] sz;
                using (MemoryStream ms = new MemoryStream())
                {
                    workbook.Write(ms);
                    sz = ms.ToArray();
                }
                var ExType = new FileExtensionContentTypeProvider().Mappings[".xlsx"];

                scope.Complete();
                return File(sz, ExType, "工作簿.xlsx");
            }

        }
        /// <summary>
        /// 角色批量导出
        /// </summary>
        /// <param name="Index"></param>
        /// <param name="Size"></param>
        /// <param name="Name"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> RoleExportAll(string? Ids)
        {
            IWorkbook workbook = new XSSFWorkbook();
            ISheet sheet = workbook.CreateSheet("工作簿");
            using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {

                try
                {

                    IRow col = sheet.CreateRow(0);
                    col.CreateCell(0).SetCellValue("角色名称");
                    col.CreateCell(1).SetCellValue("创建时间");
                    col.CreateCell(2).SetCellValue("备注");
                    List<Role> uselist = new List<Role>();
                    var data = Ids.Split(",").Select(x => int.Parse(x));
                    foreach (var id in data)
                    {
                        var slist = Role.GetQuery().Where(x => x.Role_Id == id && !x.IsDel).ToList();


                        foreach (var item in slist)
                        {
                            if (item != null)
                                uselist.Add(item);
                        }

                        for (int i = 0; i < uselist.Count(); i++)
                        {
                            IRow scol = sheet.CreateRow(i + 1);
                            scol.CreateCell(0).SetCellValue(slist[i].Role_Name);
                            scol.CreateCell(1).SetCellValue(slist[i].Role_CreateTime.ToString().Substring(0, 10));
                            scol.CreateCell(2).SetCellValue(slist[i].Role_Content);
                        }
                    }

                }
                catch (Exception)
                {
                    throw;
                }
                byte[] sz;
                using (MemoryStream ms = new MemoryStream())
                {
                    workbook.Write(ms);
                    sz = ms.ToArray();
                }
                var ExType = new FileExtensionContentTypeProvider().Mappings[".xlsx"];

                scope.Complete();
                return File(sz, ExType, "工作簿.xlsx");
            }




        }
        /// <summary>
        /// 资源批量导出
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> ResourcesesAll(string? Ids)
        {
            IWorkbook workbook = new XSSFWorkbook();
            ISheet sheet = workbook.CreateSheet("工作簿");
            using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {

                try
                {

                    IRow col = sheet.CreateRow(0);
                    col.CreateCell(0).SetCellValue("编号（唯一标识）");
                    col.CreateCell(1).SetCellValue("菜单名称");
                    col.CreateCell(2).SetCellValue("父级菜单");
                    col.CreateCell(3).SetCellValue("类型（页面/Ajax请求/对话框等）");
                    col.CreateCell(4).SetCellValue("样式（图标类名）");
                    col.CreateCell(5).SetCellValue("排序");
                    col.CreateCell(6).SetCellValue("路径");
                    col.CreateCell(7).SetCellValue("创建时间");
                    List<Resources> uselist = new List<Resources>();
                    var data = Ids.Split(",").Select(x => int.Parse(x));
                    foreach (var id in data)
                    {
                        var slist = Resources.GetQuery().Where(x => x.Menu_Id == id && !x.IsDel).ToList();


                        foreach (var item in slist)
                        {
                            if (item != null)
                                uselist.Add(item);
                        }

                        for (int i = 0; i < uselist.Count(); i++)
                        {
                            IRow scol = sheet.CreateRow(i + 1);
                            scol.CreateCell(0).SetCellValue(slist[i].Menu_Code);
                            scol.CreateCell(1).SetCellValue(slist[i].Menu_Name);
                            scol.CreateCell(2).SetCellValue(slist[i].Parent_Menu);
                            scol.CreateCell(3).SetCellValue(slist[i].Type);
                            scol.CreateCell(4).SetCellValue(slist[i].Style);
                            scol.CreateCell(5).SetCellValue(Convert.ToDouble(slist[i].Sort));
                            scol.CreateCell(6).SetCellValue(slist[i].Path);
                            scol.CreateCell(7).SetCellValue(slist[i].Created_At.ToString().Substring(0, 10));
                        }
                    }

                }
                catch (Exception)
                {
                    throw;
                }
                byte[] sz;
                using (MemoryStream ms = new MemoryStream())
                {
                    workbook.Write(ms);
                    sz = ms.ToArray();
                }
                var ExType = new FileExtensionContentTypeProvider().Mappings[".xlsx"];

                scope.Complete();
                return File(sz, ExType, "工作簿.xlsx");
            }




        }
        /// <summary>
        /// 部门批量导出
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> DepartmentAll(string? Ids)
        {
            IWorkbook workbook = new XSSFWorkbook();
            ISheet sheet = workbook.CreateSheet("工作簿");
            using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {

                try
                {

                    IRow col = sheet.CreateRow(0);
                    col.CreateCell(0).SetCellValue("部门名称");
                    col.CreateCell(1).SetCellValue("父级部门名称");
                    col.CreateCell(2).SetCellValue("创建时间");
                    List<DepartMents> uselist = new List<DepartMents>();
                    var data = Ids.Split(",").Select(x => int.Parse(x));
                    foreach (var id in data)
                    {
                        var slist = DepartMents.GetQuery().Where(x => x.Department_Id == id && !x.IsDel).ToList();


                        foreach (var item in slist)
                        {
                            if (item != null)
                                uselist.Add(item);
                        }

                        for (int i = 0; i < uselist.Count(); i++)
                        {
                            IRow scol = sheet.CreateRow(i + 1);
                            scol.CreateCell(0).SetCellValue(slist[i].Department_Name);
                            scol.CreateCell(1).SetCellValue(slist[i].Parent_Department);
                            scol.CreateCell(2).SetCellValue(slist[i].CreateTime.ToString().Substring(0, 10));
                        }
                    }

                }
                catch (Exception)
                {
                    throw;
                }
                byte[] sz;
                using (MemoryStream ms = new MemoryStream())
                {
                    workbook.Write(ms);
                    sz = ms.ToArray();
                }
                var ExType = new FileExtensionContentTypeProvider().Mappings[".xlsx"];

                scope.Complete();
                return File(sz, ExType, "工作簿.xlsx");
            }




        }
        /// <summary>
        /// 用户导出
        /// </summary>
        /// <param name="Index"></param>
        /// <param name="Size"></param>
        /// <param name="Name"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Export(int NumCout)
        {
            using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                try
                {

                    var list = Users.GetQuery();

                    var slist = list.OrderByDescending(x => x.UId).Where(c => !c.IsDel).ToList();
                    IWorkbook workbook = new XSSFWorkbook();
                    ISheet sheet = workbook.CreateSheet("工作簿");
                    IRow col = sheet.CreateRow(0);
                    col.CreateCell(0).SetCellValue("用户名");
                    col.CreateCell(1).SetCellValue("工号");
                    col.CreateCell(2).SetCellValue("真名");
                    col.CreateCell(3).SetCellValue("邮箱");
                    col.CreateCell(4).SetCellValue("手机");
                    col.CreateCell(5).SetCellValue("固定电话");
                    col.CreateCell(6).SetCellValue("创建时间");
                    col.CreateCell(7).SetCellValue("登录次数");
                    col.CreateCell(8).SetCellValue("部门");
                    col.CreateCell(9).SetCellValue("角色");
                    for (int i = 0; i < slist.Count(); i++)
                    {
                        if (NumCout > 0 && i >= NumCout)
                        {

                            break;
                        }
                        IRow scol = sheet.CreateRow(i + 1);
                        scol.CreateCell(0).SetCellValue(slist[i].User_Name);
                        scol.CreateCell(1).SetCellValue(slist[i].User_WorkCode);
                        scol.CreateCell(2).SetCellValue(Convert.ToDouble(slist[i].Login_Number));
                        scol.CreateCell(3).SetCellValue(slist[i].User_Email);
                        scol.CreateCell(4).SetCellValue(Convert.ToDouble(slist[i].User_Phone));
                        scol.CreateCell(5).SetCellValue(Convert.ToDouble(slist[i].User_FixedPhone));
                        scol.CreateCell(6).SetCellValue(slist[i].CreateTime.ToString().Substring(0, 10));
                        scol.CreateCell(7).SetCellValue(Convert.ToDouble(slist[i].Login_Number));
                        scol.CreateCell(8).SetCellValue(Convert.ToDouble(slist[i].Department_Id));
                        scol.CreateCell(9).SetCellValue(Convert.ToDouble(slist[i].Role_Id));


                    }
                    byte[] sz;
                    using (MemoryStream ms = new MemoryStream())
                    {
                        workbook.Write(ms);
                        sz = ms.ToArray();
                    }
                    var ExType = new FileExtensionContentTypeProvider().Mappings[".xlsx"];

                    scope.Complete();

                    return File(sz, ExType, "工作簿.xlsx");
                }
                catch (Exception)
                {
                    throw;
                }
            }




        }
        /// <summary>
        /// 角色导出
        /// </summary>
        /// <param name="NumCout"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> RoleExport(int NumCout)
        {
            using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                try
                {

                    var list = Role.GetQuery();

                    var slist = list.OrderByDescending(x => x.Role_Id).Where(c => !c.IsDel).ToList();
                    IWorkbook workbook = new XSSFWorkbook();
                    ISheet sheet = workbook.CreateSheet("工作簿");
                    IRow col = sheet.CreateRow(0);
                    col.CreateCell(0).SetCellValue("角色名称");
                    col.CreateCell(1).SetCellValue("创建时间");
                    col.CreateCell(2).SetCellValue("备注");

                    for (int i = 0; i < slist.Count(); i++)
                    {
                        if (NumCout > 0 && i >= NumCout)
                        {

                            break;
                        }
                        IRow scol = sheet.CreateRow(i + 1);
                        scol.CreateCell(0).SetCellValue(slist[i].Role_Name);
                        scol.CreateCell(1).SetCellValue(slist[i].Role_CreateTime.ToString().Substring(0, 10));
                        scol.CreateCell(2).SetCellValue(slist[i].Role_Content);

                    }
                    byte[] sz;
                    using (MemoryStream ms = new MemoryStream())
                    {
                        workbook.Write(ms);
                        sz = ms.ToArray();
                    }
                    var ExType = new FileExtensionContentTypeProvider().Mappings[".xlsx"];

                    scope.Complete();

                    return File(sz, ExType, "工作簿.xlsx");
                }
                catch (Exception)
                {
                    throw;
                }
            }




        }
        /// <summary>
        /// 资源导出
        /// </summary>
        /// <param name="NumCout"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> ResourcesesExport(int NumCout)
        {
            using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                try
                {

                    var list = Resources.GetQuery();

                    var slist = list.OrderByDescending(x => x.Menu_Id).Where(c => !c.IsDel).ToList();
                    IWorkbook workbook = new XSSFWorkbook();
                    ISheet sheet = workbook.CreateSheet("工作簿");
                    IRow col = sheet.CreateRow(0);
                    col.CreateCell(0).SetCellValue("编号（唯一标识）");
                    col.CreateCell(1).SetCellValue("菜单名称");
                    col.CreateCell(2).SetCellValue("父级菜单");
                    col.CreateCell(3).SetCellValue("类型（页面/Ajax请求/对话框等）");
                    col.CreateCell(4).SetCellValue("样式（图标类名）");
                    col.CreateCell(5).SetCellValue("排序");
                    col.CreateCell(6).SetCellValue("路径");
                    col.CreateCell(7).SetCellValue("创建时间");
                    for (int i = 0; i < slist.Count(); i++)
                    {
                        if (NumCout > 0 && i >= NumCout)
                        {

                            break;
                        }
                        IRow scol = sheet.CreateRow(i + 1);
                        scol.CreateCell(0).SetCellValue(slist[i].Menu_Code);
                        scol.CreateCell(1).SetCellValue(slist[i].Menu_Name);
                        scol.CreateCell(2).SetCellValue(slist[i].Parent_Menu);
                        scol.CreateCell(3).SetCellValue(slist[i].Type);
                        scol.CreateCell(4).SetCellValue(slist[i].Style);
                        scol.CreateCell(5).SetCellValue(Convert.ToDouble(slist[i].Sort));
                        scol.CreateCell(6).SetCellValue(slist[i].Path);
                        scol.CreateCell(7).SetCellValue(slist[i].Created_At.ToString().Substring(0, 10));



                    }
                    byte[] sz;
                    using (MemoryStream ms = new MemoryStream())
                    {
                        workbook.Write(ms);
                        sz = ms.ToArray();
                    }
                    var ExType = new FileExtensionContentTypeProvider().Mappings[".xlsx"];

                    scope.Complete();

                    return File(sz, ExType, "工作簿.xlsx");
                }
                catch (Exception)
                {
                    throw;
                }
            }




        }

        /// <summary>
        /// 部门导出
        /// </summary>
        /// <param name="NumCout"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> DepartmentsExport(int NumCout)
        {
            using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                try
                {

                    var list = DepartMents.GetQuery();

                    var slist = list.OrderByDescending(x => x.Department_Id).Where(c => !c.IsDel).ToList();
                    IWorkbook workbook = new XSSFWorkbook();
                    ISheet sheet = workbook.CreateSheet("工作簿");
                    IRow col = sheet.CreateRow(0);
                    col.CreateCell(0).SetCellValue("部门名称");
                    col.CreateCell(1).SetCellValue("父级部门名称");
                    col.CreateCell(2).SetCellValue("创建时间");
                    for (int i = 0; i < slist.Count(); i++)
                    {
                        if (NumCout > 0 && i >= NumCout)
                        {
                            break;
                        }
                        IRow scol = sheet.CreateRow(i + 1);
                        scol.CreateCell(0).SetCellValue(slist[i].Department_Name);
                        scol.CreateCell(1).SetCellValue(slist[i].Parent_Department);
                        scol.CreateCell(2).SetCellValue(slist[i].CreateTime.ToString().Substring(0, 10));
                    }
                    byte[] sz;
                    using (MemoryStream ms = new MemoryStream())
                    {
                        workbook.Write(ms);
                        sz = ms.ToArray();
                    }
                    var ExType = new FileExtensionContentTypeProvider().Mappings[".xlsx"];

                    scope.Complete();

                    return File(sz, ExType, "工作簿.xlsx");
                }
                catch (Exception)
                {
                    throw;
                }
            }
        }
        /// <summary>
        /// 产品出库单导出
        /// </summary>
        /// <param name="NumCout"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> ProductOutAllows(int NumCout) 
        {
            using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                try
                {

                    var list = ProductOutAllow.GetQuery();

                    var slist = list.OrderByDescending(x => x.Product_Id).Where(c => !c.IsDel).ToList();
                    IWorkbook workbook = new XSSFWorkbook();
                    ISheet sheet = workbook.CreateSheet("工作簿");
                    IRow col = sheet.CreateRow(0);
                    col.CreateCell(0).SetCellValue("产品编号");
                    col.CreateCell(1).SetCellValue("产品名称");
                    col.CreateCell(2).SetCellValue("产品规格");
                    col.CreateCell(3).SetCellValue("类别");
                    col.CreateCell(4).SetCellValue("存储单位");
                    col.CreateCell(5).SetCellValue("库存数量");
                    for (int i = 0; i < slist.Count(); i++)
                    {
                        if (NumCout > 0 && i >= NumCout)
                        {
                            break;
                        }
                        IRow scol = sheet.CreateRow(i + 1);
                        scol.CreateCell(0).SetCellValue(slist[i].Product_Code);
                        scol.CreateCell(1).SetCellValue(slist[i].Product_Name);
                        scol.CreateCell(2).SetCellValue(slist[i].Product_Spec);
                        scol.CreateCell(3).SetCellValue(slist[i].Category);
                        scol.CreateCell(4).SetCellValue(slist[i].Storage_Unit);
                        scol.CreateCell(5).SetCellValue(Convert.ToDouble(slist[i].Inventory));
                    }
                    byte[] sz;
                    using (MemoryStream ms = new MemoryStream())
                    {
                        workbook.Write(ms);
                        sz = ms.ToArray();
                    }
                    var ExType = new FileExtensionContentTypeProvider().Mappings[".xlsx"];

                    scope.Complete();

                    return File(sz, ExType, "工作簿.xlsx");
                }
                catch (Exception)
                {
                    throw;
                }
            }
        }
    }
}
